﻿using System;
using System.Collections;
using UnityEngine;

// Token: 0x02000371 RID: 881
public class SkinnedCollisionHelper : MonoBehaviour
{
	// Token: 0x060016CC RID: 5836 RVA: 0x00092F78 File Offset: 0x00091178
	public bool Init()
	{
		if (this.IsInit)
		{
			return true;
		}
		this.skinnedMeshRenderer = base.GetComponent<SkinnedMeshRenderer>();
		this.meshCollider = base.GetComponent<MeshCollider>();
		if (this.meshCollider != null && this.skinnedMeshRenderer != null)
		{
			this.meshCalc = UnityEngine.Object.Instantiate<Mesh>(this.skinnedMeshRenderer.sharedMesh);
			this.meshCalc.name = this.skinnedMeshRenderer.sharedMesh.name + "_calc";
			this.meshCollider.sharedMesh = this.meshCalc;
			this.meshCalc.MarkDynamic();
			Vector3[] vertices = this.skinnedMeshRenderer.sharedMesh.vertices;
			Vector3[] normals = this.skinnedMeshRenderer.sharedMesh.normals;
			Matrix4x4[] bindposes = this.skinnedMeshRenderer.sharedMesh.bindposes;
			BoneWeight[] boneWeights = this.skinnedMeshRenderer.sharedMesh.boneWeights;
			this.nodeWeights = new SkinnedCollisionHelper.CWeightList[this.skinnedMeshRenderer.bones.Length];
			for (int i = 0; i < this.skinnedMeshRenderer.bones.Length; i++)
			{
				this.nodeWeights[i] = new SkinnedCollisionHelper.CWeightList();
				this.nodeWeights[i].transform = this.skinnedMeshRenderer.bones[i];
			}
			for (int j = 0; j < vertices.Length; j++)
			{
				BoneWeight boneWeight = boneWeights[j];
				if (boneWeight.weight0 != 0f)
				{
					Vector3 p = bindposes[boneWeight.boneIndex0].MultiplyPoint3x4(vertices[j]);
					Vector3 n = bindposes[boneWeight.boneIndex0].MultiplyPoint3x4(normals[j]);
					this.nodeWeights[boneWeight.boneIndex0].weights.Add(new SkinnedCollisionHelper.CVertexWeight(j, p, n, boneWeight.weight0));
				}
				if (boneWeight.weight1 != 0f)
				{
					Vector3 p2 = bindposes[boneWeight.boneIndex1].MultiplyPoint3x4(vertices[j]);
					Vector3 n2 = bindposes[boneWeight.boneIndex1].MultiplyPoint3x4(normals[j]);
					this.nodeWeights[boneWeight.boneIndex1].weights.Add(new SkinnedCollisionHelper.CVertexWeight(j, p2, n2, boneWeight.weight1));
				}
				if (boneWeight.weight2 != 0f)
				{
					Vector3 p3 = bindposes[boneWeight.boneIndex2].MultiplyPoint3x4(vertices[j]);
					Vector3 n3 = bindposes[boneWeight.boneIndex2].MultiplyPoint3x4(normals[j]);
					this.nodeWeights[boneWeight.boneIndex2].weights.Add(new SkinnedCollisionHelper.CVertexWeight(j, p3, n3, boneWeight.weight2));
				}
				if (boneWeight.weight3 != 0f)
				{
					Vector3 p4 = bindposes[boneWeight.boneIndex3].MultiplyPoint3x4(vertices[j]);
					Vector3 n4 = bindposes[boneWeight.boneIndex3].MultiplyPoint3x4(normals[j]);
					this.nodeWeights[boneWeight.boneIndex3].weights.Add(new SkinnedCollisionHelper.CVertexWeight(j, p4, n4, boneWeight.weight3));
				}
			}
			this.UpdateCollisionMesh(false);
			this.IsInit = true;
			return true;
		}
		return false;
	}

	// Token: 0x060016CD RID: 5837 RVA: 0x000932FB File Offset: 0x000914FB
	public bool Release()
	{
		UnityEngine.Object.Destroy(this.meshCalc);
		return true;
	}

	// Token: 0x060016CE RID: 5838 RVA: 0x0009330C File Offset: 0x0009150C
	public void UpdateCollisionMesh(bool _bRelease = true)
	{
		Vector3[] vertices = this.meshCalc.vertices;
		for (int i = 0; i < vertices.Length; i++)
		{
			vertices[i] = Vector3.zero;
		}
		foreach (SkinnedCollisionHelper.CWeightList cweightList in this.nodeWeights)
		{
			Matrix4x4 localToWorldMatrix = cweightList.transform.localToWorldMatrix;
			IEnumerator enumerator = cweightList.weights.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					object obj = enumerator.Current;
					SkinnedCollisionHelper.CVertexWeight cvertexWeight = (SkinnedCollisionHelper.CVertexWeight)obj;
					vertices[cvertexWeight.index] += localToWorldMatrix.MultiplyPoint3x4(cvertexWeight.localPosition) * cvertexWeight.weight;
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
		}
		for (int k = 0; k < vertices.Length; k++)
		{
			vertices[k] = base.transform.InverseTransformPoint(vertices[k]);
		}
		this.meshCalc.vertices = vertices;
		this.meshCollider.enabled = false;
		this.meshCollider.enabled = true;
	}

	// Token: 0x04001965 RID: 6501
	private bool IsInit;

	// Token: 0x04001966 RID: 6502
	private SkinnedCollisionHelper.CWeightList[] nodeWeights;

	// Token: 0x04001967 RID: 6503
	private SkinnedMeshRenderer skinnedMeshRenderer;

	// Token: 0x04001968 RID: 6504
	private MeshCollider meshCollider;

	// Token: 0x04001969 RID: 6505
	private Mesh meshCalc;

	// Token: 0x02000372 RID: 882
	private class CVertexWeight
	{
		// Token: 0x060016CF RID: 5839 RVA: 0x0009346C File Offset: 0x0009166C
		public CVertexWeight(int i, Vector3 p, Vector3 n, float w)
		{
			this.index = i;
			this.localPosition = p;
			this.localNormal = n;
			this.weight = w;
		}

		// Token: 0x0400196A RID: 6506
		public int index;

		// Token: 0x0400196B RID: 6507
		public Vector3 localPosition;

		// Token: 0x0400196C RID: 6508
		public Vector3 localNormal;

		// Token: 0x0400196D RID: 6509
		public float weight;
	}

	// Token: 0x02000373 RID: 883
	private class CWeightList
	{
		// Token: 0x060016D0 RID: 5840 RVA: 0x00093491 File Offset: 0x00091691
		public CWeightList()
		{
			this.weights = new ArrayList();
		}

		// Token: 0x0400196E RID: 6510
		public Transform transform;

		// Token: 0x0400196F RID: 6511
		public ArrayList weights;
	}
}
